<template>
  <div class="page-table">
    <xt-table
      :listCount="dataListCount"
      v-model:page="pageInfo"
      v-bind="pageTabelConfig"
      :listData="dateList"
      :isCreate="isCreate"
    >
      <!-- 除了公共插槽,动态注册个性化插槽 -->
      <template #handerHandler v-if="pageTabelConfig.title !== ''">
        <el-button @click="showEmptyDialog" size="mini" type="primary" v-if="isCreate"
          >新建{{ pageTabelConfig.title.slice(0, 2) }}</el-button
        >
      </template>
      <template v-for="item in otherPropSlots" :key="item.prop" #[item.prop]="scope">
        <slot :name="item.prop" :row="scope.row"></slot>
      </template>

      <!-- 公共插槽 -->
      <template #enable="scope">
        <el-button size="mini" type="danger" v-if="scope.row.enable !== 1">禁用</el-button>
        <el-button size="mini" type="primary" v-else>启用</el-button>
      </template>

      <template #createAt="scope">
        <strong>{{ $filters.dateFormatUTC(scope.row.createAt) }}</strong>
      </template>
      <template #updateAt="scope">
        <strong>{{ $filters.dateFormatUTC(scope.row.updateAt) }}</strong>
      </template>

      <template #handle="scope">
        <!-- 编辑 -->
        <el-button @click="editRow(scope.row)" type="primary" size="mini" v-if="isUpdate">编辑</el-button>

        <!-- 删除 -->
        <el-popconfirm
          confirm-button-text="确认"
          cancel-button-text="取消"
          :icon="InfoFilled"
          icon-color="red"
          title="确定删除?"
          @confirm="confirmEvent(scope.row)"
          @cancel="cancelEvent"
        >
          <template #reference>
            <el-button type="danger" size="mini" v-if="isDelete">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </xt-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from "vue"
import { useStore } from "vuex"
import { IRootWithModel } from "@/types/userType"
import XtTable from "@/base_ui/table/index"
import usePermission from "@/hook/usePermission"
import { InfoFilled } from "@element-plus/icons-vue"
import { ElMessage } from "element-plus"
export default defineComponent({
  components: {
    XtTable,
  },
  props: {
    pageName: {
      type: String,
      required: true,
    },
    pageTabelConfig: {
      type: Object,
      required: true,
    },
  },
  emits: ["pageTableEvent", "showEmptyDialog"],
  setup(props, { emit }) {
    const isCreate = usePermission(props.pageName, "create")
    const isDelete = usePermission(props.pageName, "delete")
    const isUpdate = usePermission(props.pageName, "update")
    const isQuery = usePermission(props.pageName, "query")
    console.log(isCreate)
    console.log(isDelete)
    console.log(isUpdate)
    console.log(isQuery)
    const store = useStore<IRootWithModel>()
    // 双向绑定的 页码和页面显示的数量对象
    const pageInfo = ref({ currentPage: 1, size: 10 })
    const getPageDate = (Info: any = {}) => {
      if (!isQuery) return
      store.dispatch("system/getPageInfoAction", {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.size,
          size: pageInfo.value.size,
          ...Info,
        },
      })
    }

    // 动态注册插槽
    const otherPropSlots = props.pageTabelConfig.propList.filter((item: any) => {
      if (item.prop == "enable") return false
      if (item.prop == "createAt") return false
      if (item.prop == "updateAt") return false
      if (item.prop == "handle") return false
      return true
    })
    console.log(otherPropSlots)
    watch(pageInfo, (newValue) => {
      console.log("监测到页码发生改变")
      getPageDate()
    })

    getPageDate()

    // 从store中取出数据

    const dateList = computed(() => store.getters[`system/pageDate`](props.pageName))
    const dataListCount = computed(() => store.getters[`system/pageDateCount`](props.pageName))

    // 确认删除
    const confirmEvent = async (row: any) => {
      console.log(row)
      const res = await store.dispatch("system/deletPageInfoAction", {
        pageName: props.pageName,
        id: row.id,
      })
      console.log(res)
      if (res.includes("成功")) {
        ElMessage({
          message: res,
          type: "success",
        })
        const deletIndex = dateList.value.findIndex((item: any) => {
          return item.id == row.id
        })
        // 直接从本地列表删除
        dateList.value.splice(deletIndex, 1)
      } else {
        ElMessage({
          message: res,
          type: "error",
        })
      }
    }
    // 取消按钮
    const cancelEvent = () => {
      return false
    }

    // 编辑按钮
    const editRow = (row: any) => {
      emit("pageTableEvent", row)
    }

    // 新建按钮
    const showEmptyDialog = () => {
      // 拷贝对象,主要用到对象的key
      const emptyRow: any = { ...dateList.value[0] }
      Object.keys(emptyRow).forEach((key) => {
        emptyRow[key] = ""
      })
      emit("showEmptyDialog", emptyRow)
    }

    return {
      editRow,
      XtTable,
      dateList,
      getPageDate,
      dataListCount,
      pageInfo,
      otherPropSlots,
      isCreate,
      isDelete,
      isUpdate,
      InfoFilled,
      confirmEvent,
      cancelEvent,
      showEmptyDialog,
    }
  },
})
</script>
<style lang="less" scoped></style>
